package com.company.calculate;

import java.util.Scanner;

/**
 * Created by v on 17-6-12.
 */
public class Stock {
    private static double a;
    private static double b;

    private static int n;

    private static double[] x0;
    private static double[] x1;
    private static double[] z;
    private static double[] p;

    private static double[][] B;
    private static double[][] Y;

    private static double ans;

    private static void calX1(){
        x1=new double[x0.length];

        for(int i=0;i<x0.length;i++){
            x1[i]=0;
            for(int j=0;j<=i;j++){
                x1[i]+=x0[j];
            }
        }
    }

    private static void calZ(){
        z=new double[x0.length];
        for(int i=1;i<z.length;i++){
            z[i]=0.4*x1[i]+0.6*x1[i-1]*(i-1);
        }
    }

    private static void calP(){
        p=new double[x0.length];

        for(int i=1;i<p.length;i++){
            p[i]=x0[i-1]/x0[i];
        }
    }

    private static double[][] T(double[][] matrix){
        double[][] t=new double[matrix[0].length][matrix.length];

        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[i].length;j++){
                t[j][i]=matrix[i][j];
            }
        }

        return t;
    }

    private static double[][] multiplication(double[][] matrix1,double[][] matrix2){
        double[][] matrix=new double[matrix1.length][matrix2[0].length];

        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[i].length;j++){
                matrix[i][j]=0;

                for(int k=0;k<matrix1[i].length;k++){
                    matrix[i][j]+=matrix1[i][k]*matrix2[k][j];
                }
            }
        }

        return matrix;
    }

    private static void solve(){
        B=new double[2][n-1];
        for(int i=1;i<n;i++){
            B[0][i-1]=-z[i];
            B[1][i-1]=1;
        }
        B=T(B);

        Y=new double[1][n-1];
        for(int i=1;i<n;i++){
            Y[0][i-1]=x0[i];
        }

        Y=T(Y);

        double[][] matrix=multiplication(inverse(multiplication(T(B),B)),multiplication(T(B),Y));

        a=matrix[0][0];
        b=matrix[1][0];
    }

    private static void calAns(){
        ans=(x0[0]-b/a)*(Math.pow(Math.E,-a*n)-Math.pow(Math.E,-a*(n-1)));
    }

    private static double[][] inverse(double[][] matrix){
        double a=matrix[0][0];
        double b=matrix[0][1];
        double c=matrix[1][0];
        double d=matrix[1][1];

        double p=1/(a*d-b*c);
        matrix[0][0]=d*p;
        matrix[0][1]=-b*p;
        matrix[1][0]=-c*p;
        matrix[1][1]=a*p;

        return matrix;
    }

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入样本数：");

        n=input.nextInt();
        System.out.println("请分别输入"+n+"个样本");
        x0=new double[n];
        for(int i=0;i<n;i++){
            x0[i]=input.nextDouble();
        }
        calX1();
        calP();
        calZ();
        solve();

        calAns();

        for(int i=1;i<n;i++){
            System.out.print(p[i]+" ");
        }

        System.out.println();

        System.out.println("ans="+ans);
    }
}
